package com.fs.adapters;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentmanagersystem.R;
import com.fs.po.StuSocre;

/**
 * 学生查询成绩适配器
 * @author chenwenfei
 *
 */
public class StuSearchGradeAdapter extends BaseAdapter {

	private Context context;
	List<StuSocre> datalist;
	private int len=0;
	
	public StuSearchGradeAdapter(Context context) {

		this.context=context;
	}

	
	
	/**
	 * setData为适配器设置数据
	 */
	public void setData(List<StuSocre> datalist) {
		
		this.datalist=datalist;
		
		this.len=datalist.size();
		
		Toast.makeText(context, ""+datalist.size(), 0).show();
		
	}
	
	
	@Override
	public int getCount() {

		return len;
	}

	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {

		Holder holder=null;
		if(view==null){
			view = ((Activity) context).getLayoutInflater().inflate(R.layout.student_search_grade_tab, null);
			holder = new Holder();// 创建内部类对象，保存列表项中子控件的引用；
			
			holder.classNameText=(TextView) view.findViewById(R.id.student_search_classesName);
			holder.scoreNameText=(TextView) view.findViewById(R.id.student_search_gradeSize);
			holder.pjTextView=(TextView) view.findViewById(R.id.student_search_grade_pjtv);
			holder.pjImageBtn=(ImageButton) view.findViewById(R.id.student_search_grade_detil);
			
			view.setTag(holder);
			
		}else if (view != null) {
			holder=(Holder) view.getTag();
		  }
			holder.classNameText.setText(this.datalist.get(position).getName());
			holder.scoreNameText.setText(""+this.datalist.get(position).getScore());
			holder.pjTextView.setText(""+this.datalist.get(position).getPj());
			
			holder.pjImageBtn.setOnClickListener(new MyImageButtonListener(holder.pjTextView));
			
			return view;
		
	}
	
	class Holder{
		TextView classNameText;
		TextView scoreNameText;
		ImageButton pjImageBtn;
		TextView pjTextView;
		 
	}
	
}

class MyImageButtonListener implements OnClickListener{

	@Override
	public void onClick(View v) {
		
		if (pjtv.getVisibility() == pjtv.VISIBLE) {
			pjtv.setVisibility(pjtv.GONE);
		} else {
			pjtv.setVisibility(pjtv.VISIBLE);
		}
		
	}
	
	private TextView pjtv;
	public MyImageButtonListener(TextView pjtv) {
		this.pjtv=pjtv;
	}
	
	
	
}

